package org.jetlinks.community.mstn.service.terms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.EmptySqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.PrepareSqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.SqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.term.AbstractTermFragmentBuilder;
import org.hswebframework.web.exception.BusinessException;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@Component
public class MstnAssetsTermBuilder extends AbstractTermFragmentBuilder {
    public static final String assetsType = "mstn-assets";

    public MstnAssetsTermBuilder() {
        super(assetsType, "查询资产设备");
    }


    private void acceptTerm(boolean and, PrepareSqlFragments fragments, Map<String , Object> terms) {
        String assetType = String.valueOf(terms.get("assetType"));
        fragments.addSql("and").addSql("ma.asset_type = ?").addParameter(assetType);
        List<Map<String ,String >> targets = (List<Map<String, String>>) terms.get("targets");
        acceptTerm(and,fragments,targets);

    }

    private void acceptTerm(boolean and, PrepareSqlFragments fragments, List<Map<String, String>> targets) {
        int len = 0;
        fragments.addSql("and (");
        for (Map<String ,String > target : targets) {
            if (len ++ > 0) {
                fragments.addSql(and ? "and" : "or");
            }
            //类型 org or role
            String assetTargetType = String.valueOf(target.get("type"));
            //机构id
            String assetTarget = String.valueOf(target.get("id"));
            fragments
                .addSql("ma.asset_target_type = ?").addParameter(assetTargetType)
                .addSql("and").addSql("ma.asset_target = ?").addParameter(assetTarget)
                .addSql("and").addSql("ma.state = 1");
        }
        fragments.addSql(")");
    }

    @Override
    public SqlFragments createFragments(String columnFullName, RDBColumnMetadata rdbColumnMetadata, Term term) {
        List<Object> values = this.convertList(rdbColumnMetadata, term);
        if (values.isEmpty()) {
            return EmptySqlFragments.INSTANCE;
        } else {
            PrepareSqlFragments fragments = PrepareSqlFragments.of();
            if (term.getOptions().contains("not")) {
                fragments.addSql("not");
            }
            fragments.addSql("exists( select 1 from m_dimension_asset ma where ma.asset_id =" + columnFullName);
            Object value = term.getValue();
            boolean and = term.getOptions().contains("and");
            if (value instanceof Map) {
                acceptTerm(and, fragments, (Map<String, Object>) value);
            } else if (value instanceof List) {
                acceptTerm(and, fragments, (List<Map<String, String>>) value);
            } else {
                String terms = String.valueOf(value);
                //json
                if (terms.startsWith("[")) {
                    if (term.getOptions().contains("in")) {
//                        fragments.addSql("and ma.asset_target in ")
                    } else {
                        System.out.println("暂未实现");
                    }

                } else if (terms.startsWith("{")) {
                    System.out.println("暂未实现");
                } else if (terms.contains(":") && !terms.contains(" ")) {
                    System.out.println("暂未实现");
                } else {
                    System.out.println("暂未实现");
                    //SQL表达式
    //                List<Term> tagKeys = TermExpressionParser.parse(terms);
                }
            }

            fragments.addSql(")");
            return fragments;
        }
    }


}
